import { _decorator, Animation, AnimationClip } from "cc";
import State from "../../Base/State";
import StateMachine, { getInitParamsTrigger } from "../../Base/StateMachine";
import { EntityTypeEnum } from "../../Common";
import { EntityStateEnum, ParamsNameEnum } from "../../Enum";
const { ccclass } = _decorator;

@ccclass("BulletStateMachine")
export class BulletStateMachine extends StateMachine {
  init(type: EntityTypeEnum) {
    this.type = type;
        //每次init的时候都会挂载脚本，所以在对象池调用时要判断有没有这个组件，有就不add了
    this.animationComponent =this.node.getComponent(Animation)|| this.node.addComponent(Animation);
    this.initParams();
    this.initStateMachines();
    this.initAnimationEvent();
  }
  /**
   * 初始化状态机参数，添加状态机参数
   */
  initParams() {
    this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger());
  }
  /**
   * 初始化状态机,new State的构造方法需要传入当前状态机对象，动画路径和动画循环方式
   */
  initStateMachines() {
    this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`));
  }

  initAnimationEvent() {}
  
  /**
   * 两个状态之间的条件判断，根据当前状态机params的值来决定currentState当前状态机状态
   */
  run() {
    switch (this.currentState) {
      case this.stateMachines.get(ParamsNameEnum.Idle):
        if (this.params.get(ParamsNameEnum.Idle).value) {
          this.currentState = this.stateMachines.get(ParamsNameEnum.Idle);
        }else {
          this.currentState = this.currentState;
        }
        break;
      default:
        this.currentState = this.stateMachines.get(ParamsNameEnum.Idle);
        break;
    }
  }
}
